/**
 * Created by JetBrains WebStorm.
 * User: Administrator
 * Date: 13-3-14
 * Time: 上午12:41
 * To change this template use File | Settings | File Templates.
 */
function addLoadEvent(func){
    var old = window.onload;
    if(typeof window.onload != 'function'){
        window.onload = func;
    }else{
        window.onload = function(){
            old();
            func();
        }
    }
}

function insertAfter(newElement,targetElement){
    var parent = targetElement.parentElement;
    if(parent.lastchild == targetElement){
        parent.appendChild(newElement);
    }else{
        parent.insertBefore(newElement,targetElement.nextSibling);
    }
}

function addClass(element,value){
    if(!element.className){
        element.className = value;
    }else{
        var newClassName = element.className;
        newClassName += " ";
        newClassName += value;
        element.className = newClassName;
    }
}

function highlightPage(){
    if(!document.getElementsByTagName) return false;
    if(!document.getElementById) return false;

    var headers = document.getElementsByTagName('header');
    if(headers.length == 0) return false;
    var navs = headers[0].getElementsByTagName('nav');
    if(navs.length == 0) return false;
    var links = navs[0].getElementsByTagName('a');
    var linkurl;
//    alert(links.length);
    for(var i=0;i<links.length;i++){
//    for(var i in links){
//        alert(i);
//        alert(links[i].getAttribute('href'));
        linkurl = links[i].getAttribute('href');
        if(window.location.href.indexOf(linkurl) != -1){
            links[i].className = 'here';
            var linktext = links[i].lastChild.nodeValue.toLowerCase();
            document.body.setAttribute('id',linktext);
        }
    }
}

function moveElement(elementID,desX,desY,interval){
    //alert(elementID);
    if(!document.getElementById) return false;
    if(!document.getElementById(elementID)) return false;
    var elem = document.getElementById(elementID);
    if(elem.movement){
        clearTimeout(elem.movement);
    }
    if(!elem.style.left){
        elem.style.left = '0px';
    }
    if(!elem.style.top){
        elem.style.top = '0px';
    }
    var xpos = parseInt(elem.style.left);
    var ypos = parseInt(elem.style.top);
    var dist = 0;
    if(xpos == desX && ypos == desY){
        return true;
    }
    if(xpos < desX){
        dist = Math.ceil((desX - xpos)/10);
        xpos = xpos + dist;
    }
    if(xpos > desX){
        dist = Math.ceil((xpos - desX)/10);
        xpos = xpos - dist;
    }
    if(ypos < desY){
        dist = Math.ceil((desY - ypos)/10);
        ypos = ypos + dist;
    }
    if(ypos > desY){
        dist = Math.ceil((ypos - desY)/10);
        ypos = ypos - dist;
    }
    elem.style.left = xpos + 'px';
    elem.style.top = ypos + 'px';
//    var repeat = 'moveElement('+elementID+','+desX+','+desY+','+interval+')';
    var repeat = "moveElement('"+elementID+"',"+desX+","+desY+","+interval+")";
    elem.movement = setTimeout(repeat,interval);
}

function prepareSlideshow(){
    if(!document.getElementsByTagName) return false;
    if(!document.getElementById) return false;
    if(!document.getElementById('intro')) return false;
    var intro = document.getElementById('intro');
    var slideshow = document.createElement('div');
    slideshow.setAttribute('id','slideshow');
    var frame = document.createElement('img');
    frame.setAttribute('src','images/frame.gif');
    frame.setAttribute('alt','');
    frame.setAttribute('id','frame');
    slideshow.appendChild(frame);
    var preview = document.createElement('img');
    preview.setAttribute('src','images/slideshow.gif');
    preview.setAttribute('alt','slideshow.gif');
    preview.setAttribute('id','preview');
    slideshow.appendChild(preview);
    insertAfter(slideshow,intro);

    var links = document.getElementsByTagName('a');
    var destination;
    for(var i=0;i<links.length;i++){
        links[i].onmouseover = function(){
            destination = this.getAttribute('href');
            if(destination.indexOf('index.html') != -1){
                moveElement('preview',0,0,5);
            }else if(destination.indexOf('about.html') != -1){
                moveElement('preview',-150,0,5);
            }else if(destination.indexOf('photos.html') != -1){
                moveElement('preview',-300,0,5);
            }else if(destination.indexOf('live.html') != -1){
                moveElement('preview',-450,0,5);
            }else if(destination.indexOf('contact.html') != -1){
                moveElement('preview',-600,0,5);
            }
        }
    }
}

function showSection(id){
    var sections = document.getElementsByTagName('section');
    for(var i=0;i<sections.length;i++){
        if(sections[i].getAttribute('id') != id){
            sections[i].style.display = 'none';
        }else{
            sections[i].style.display = 'block';
        }
    }
}

function prepareIntervalnav(){
    if(!document.getElementsByTagName) return false;
    if(!document.getElementById) return false;
    var articles = document.getElementsByTagName('article');
    if(articles.length == 0) return false;
    var navs = articles[0].getElementsByTagName('nav');
    if(navs.length == 0) return false;
    var nav = navs[0];
    var links = nav.getElementsByTagName('a');
    for(var i=0;i<links.length;i++){
        var sectionId = links[i].getAttribute('href').split('#')[1];
        if(!document.getElementById(sectionId)) continue;
        document.getElementById(sectionId).style.display = 'none';
        links[i].destination = sectionId;
        links[i].onclick = function(){
            showSection(this.destination);
            return false;
        }
    }
}

function focusLabels(){
    if(!document.getElementsByTagName) return false;
    var labels = document.getElementsByTagName('label');
    for(var i=0;i<labels.length;i++){
        if(!labels[i].getAttribute('for')) continue;
        labels[i].onclick = function(){
            var id = labels[i].getAttribute('for');
            if(!document.getElementById(id)) return false;
            document.getElementById(id).focus();
        }
    }
}

function resetFeilds(whichform){
    if(document.input.placeholder) return;
    alert(1);
    for(var i=0;i<whichform.elements.length;i++){
        var element = whichform.elements[i];
        if(element.type == 'submit') continue;
        var check = element.placeholder || element.getAttribute('placeholder');
        if(!check) continue;
        element.onfocus = function(){
            var text = this.placeholder || this.getAttribute('placeholder');
            if(text == this.value){
                this.className = '';
                this.value = '';
            }
        };
        element.onblur = function(){
            if(this.value == ''){
                this.value = this.placeholder || this.getAttribute('placeholder');
                this.className = 'placeholder';
            }
        };
        element.onblur();

    }
}

function prepareForms(){
    for(var i=0;i<document.forms.length;i++){
        var thisform = document.forms[i];
        resetFeilds(thisform);
    }
}

addLoadEvent(highlightPage);
addLoadEvent(prepareSlideshow);
addLoadEvent(prepareIntervalnav);
addLoadEvent(focusLabels);
addLoadEvent(prepareForms);